﻿using IcsSEMCtrl.Service.Logger;
using IcsSEMCtrl.Util.Client;
using Lifter;
using Lifter.Device;
using Lifter.Device.Service;
using Lifter.Global.Device;
using Lifter.Gui.Monitor.Panels;
using Lifter.Utility;
using System.Collections.Generic;
using System.Windows.Forms;
using TKT.Lifter.CIM.Lifter.Device.Service;
using TKT.Lifter.CIM.Lifter.Global;
using TKT.Lifter.CIM.Lifter.Gui.Monitor.Panels;

namespace Sunny.UI.Demo
{
    public partial class FLogin : UILoginForm
    {
        LifterSEM LftSem = (LifterSEM) Naming.GetDeviceManager().GetDevice();
        MainTopPanel topPanel = (MainTopPanel)SimpleCache.GetObject("FormMain", "MainTopPanel");
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000; // 用双缓冲绘制窗口的所有子控件
                return cp;
            }
        }
        public FLogin()
        {
            InitializeComponent();
        }

        private void FLogin_ButtonLoginClick(object sender, System.EventArgs e)
        {
            UserManager usermanager = UserManager.GetUserManager();
            if (this.UserName != "")
            {
                List<UserDao> lists = usermanager.Select(UserName);
                if (lists.Count == 0)
                {
                    ShowErrorTip("该用户不存在，请先创建！！");
                }
                else
                {
                    if (UserName.Equals(lists[0].USERID1) && Password.Equals(lists[0].USERPASSWORD1))
                    {
                        this.LftSem.UserPrivilige = true;
                        this.LftSem.LoginTimeCount = 1;
                        this.LftSem.UserName = UserName;
                        base.Hide();
                        string Message = string.Concat("/"+UserName, "/登录成功", "/", UserName + "进行了登录操作");
                        this.topPanel.lbUserName.Text = UserName;
                        LogHelper.operationLogger(Message);
                        LftSem.InsertUserOperation(LftSem.UserName, Message);

                        if (1 == lists[0].PRIVILEGE1)
                        {
                            this.LftSem.UserPower = enumUserManage.Operation.ToString();
                        }
                        else if (2 == lists[0].PRIVILEGE1)
                        {
                            this.LftSem.UserPower = enumUserManage.Management.ToString();
                        }
                        if (this.rdCN.Checked)
                        {
                            this.LftSem.LanguageType = "zh";
                        }
                        else if (this.rdEN.Checked)
                        {
                            this.LftSem.LanguageType = "en";
                        }

                        //初始化主界面
                        LifterMainForm lifterMainForm = (LifterMainForm)SimpleCache.GetObject("FormMain", "LifterMainForm");
                        lifterMainForm.InitPowerControl();
                        lifterMainForm.ApplyResource(lifterMainForm);
                        //初始化
                        AllConfigControl allConfigControl = (AllConfigControl)SimpleCache.GetObject("FormMain", "AllConfigControl");
                        allConfigControl.InitPowerMenu();
                        AllHistoryControl allHistoryControl = (AllHistoryControl)SimpleCache.GetObject("FormMain", "AllHistoryControl");
                        allHistoryControl.InitPowerMenu();
                        SystemsDisplay systemsDisplay = (SystemsDisplay)SimpleCache.GetObject("FormMain", "SystemsDisplay");
                        systemsDisplay.InitPowerMenu();
                        InformPanel informPanel = (InformPanel)SimpleCache.GetObject("FormMain", "InformPanel");
                        informPanel.InitPowerMenu();
                    }
                    else
                    {
                        ShowErrorTip("登录失败，请检查用户名或密码！！");
                    }
                }
            }
            else
            {
                ShowErrorTip("请输入用户名密码！！");
            }
        }

        private void uiSymbolButton2_Click(object sender, System.EventArgs e)
        {
            UserManager usermanager = UserManager.GetUserManager();
            if (this.UserName != "")
            {
                List<UserDao> lists = usermanager.Select(UserName);
                if (lists.Count == 0)
                {
                    ShowErrorTip("该用户不存在，请先创建！！");
                }
                else
                {
                    if (UserName.Equals(lists[0].USERID1) && Password.Equals(lists[0].USERPASSWORD1))
                    {
                        this.LftSem.UserPrivilige = false;
                        this.LftSem.LoginTimeCount = 0;
                        base.Hide();
                        string Message = string.Concat("/"+ this.UserName, "/登出成功", "/", this.UserName + "进行了登出操作");
                        this.topPanel.lbUserName.Text = "未登录";
                        LogHelper.operationLogger(Message);
                        LftSem.InsertUserOperation(LftSem.UserName, Message);
                        this.LftSem.UserName = "";

                        this.LftSem.UserPower = enumUserManage.Operation.ToString();
                        LifterMainForm lifterMainForm = (LifterMainForm)SimpleCache.GetObject("FormMain", "LifterMainForm");
                        lifterMainForm.InitPowerControl();

                        //初始化
                        AllConfigControl allConfigControl = (AllConfigControl)SimpleCache.GetObject("FormMain", "AllConfigControl");
                        allConfigControl.InitPowerMenu();
                        AllHistoryControl allHistoryControl = (AllHistoryControl)SimpleCache.GetObject("FormMain", "AllHistoryControl");
                        allHistoryControl.InitPowerMenu();
                        SystemsDisplay systemsDisplay = (SystemsDisplay)SimpleCache.GetObject("FormMain", "SystemsDisplay");
                        systemsDisplay.InitPowerMenu();
                        InformPanel informPanel = (InformPanel)SimpleCache.GetObject("FormMain", "InformPanel");
                        informPanel.InitPowerMenu();
                    }
                    else
                    {
                        ShowErrorTip("登出失败，请检查用户名或密码！！");
                    }
                }
            }
            else
            {
                ShowErrorTip("请输入用户名密码！！");
            }
        }

        private void btCancel_Click(object sender, System.EventArgs e)
        {
            base.Hide();
            this.UserName = "";
            this.Password = "";
        }
    }
}